{% extends './layout/default' %}
{% block breadcrumbs %}
    {{ Breadcrumbs.render(Route.getCurrentRoute.getName) }}
{% endblock %}
{% block content %}

    <div class="row">
        <div class="col-lg-12 col-md-12 col-sm-12">
            <p>
                <a href="{{ route('rule-groups.create') }}" id="new_rule_group" class="btn btn-success">{{ 'new_rule_group'|_ }}</a>
            </p>
        </div>
    </div>

    {% if ruleGroups|length == 1 and ruleGroups[0].rules.count() == 0 %}
    {% include 'partials.empty' with {objectType: 'default', type: 'rules',route: route('rules.create')} %}
    {# make FF ignore demo for now. #}
    {% set shownDemo = true %}
    {% endif %}

    {% for ruleGroup in ruleGroups %}
        <div class="row">
            <div class="col-lg-12 col-md-12 col-sm-12">
                <div class="box rules-box" data-group="{{ ruleGroup.id }}">
                    <div class="box-header with-border">
                        <h3 class="box-title">
                            {% if ruleGroup.active %}
                                {{ ruleGroup.title }}
                            {% else %}
                                <s>{{ ruleGroup.title }}</s> ({{ 'inactive'|_|lower }})
                            {% endif %}
                        </h3>


                        <div class="box-tools pull-right">
                            <div class="btn-group">
                                <button type="button" class="btn btn-box-tool" data-widget="collapse"><span class="fa fa-minus"></span>
                                </button>
                                <button class="btn btn-box-tool dropdown-toggle" data-toggle="dropdown"><span
                                            class="fa fa-ellipsis-v"></span></button>
                                <ul class="dropdown-menu" role="menu">
                                    <li><a href="{{ route('rule-groups.edit',ruleGroup.id) }}"><span class="fa fa-fw fa-pencil"></span> {{ 'edit'|_ }}</a></li>
                                    <li><a href="{{ route('rule-groups.delete',ruleGroup.id) }}"><span class="fa fa-fw fa-trash"></span> {{ 'delete'|_ }}</a></li>
                                    <li><a href="{{ route('rule-groups.select-transactions',ruleGroup.id) }}"><span
                                                    class="fa fa-fw fa-power-off"></span> {{ trans('firefly.apply_rule_group_selection', {title: ruleGroup.title}) }}
                                        </a></li>
                                    {% if ruleGroup.order > 1 %}
                                        <li><a href="#" class="move-group" data-direction="up" data-id="{{ ruleGroup.id }}"><span
                                                        class="fa fa-fw fa-arrow-up"></span> {{ 'move_rule_group_up'|_ }}</a></li>
                                    {% endif %}
                                    {% if ruleGroup.order < ruleGroups|length %}
                                        <li><a href="#" class="move-group" data-direction="down" data-id="{{ ruleGroup.id }}"><span
                                                        class="fa fa-fw fa-arrow-down"></span> {{ 'move_rule_group_down'|_ }}
                                            </a></li>
                                    {% endif %}
                                </ul>
                            </div>
                        </div>

                    </div>
                    <div class="box-body">
                        <p>
                            <em>{{ ruleGroup.description }}</em>
                        </p>

                        {% if ruleGroup.rules.count() > 0 %}
                            <table class="table table-hover table-striped group-rules">
                                <thead>
                                <tr>
                                    <th style="width:5%;">&nbsp;</th>
                                    <th style="width:10%;">&nbsp;</th>
                                    <th style="width:10%;">&nbsp;</th>
                                    <th style="width:25%;">{{ 'rule_name'|_ }}</th>
                                    <th style="width:25%;" class="hidden-xs">{{ 'rule_triggers'|_ }}</th>
                                    <th style="width:25%;" class="hidden-xs">{{ 'rule_actions'|_ }}</th>
                                </tr>
                                </thead>
                                <tbody class="rule-connected-list">
                                {% for rule in ruleGroup.rules %}
                                    <tr class="single-rule" data-order="{{ rule.order }}"  data-id="{{ rule.id }}" data-group-id="{{ ruleGroup.id }}" data-position="{{ loop.index0 }}">
                                        <td>
                                            <div class="btn-group btn-group-xs prio_buttons">
                                                <span class="fa fa-fw fa-bars rule-handle"></span>
                                            </div>
                                        </td>
                                        <td>
                                            <div class="btn-group btn-group-xs edit_buttons">
                                                <a title="{{ 'edit'|_ }}" href="{{ route('rules.edit', rule.id) }}"
                                                   class="btn btn-default"><span
                                                            class="fa fa-fw fa-pencil"></span></a>
                                                <a title="{{ 'delete'|_ }}"
                                                   href="{{ route('rules.delete', rule.id) }}"
                                                   class="btn btn-danger"><span
                                                            class="fa fa-fw fa-trash"></span></a>
                                            </div>
                                        </td>
                                        <td>
                                            <div class="btn-group btn-group-xs test_buttons">
                                                {% if rule.active %}
                                                    {# show which transactions would match #}
                                                    <a href="{{ route('rules.search',rule.id) }}" class="btn btn-default {% if false == rule.strict %}test_rule_triggers{% endif %}" data-id="{{ rule.id }}" title="{{ 'test_rule_triggers'|_ }}"><span data-id="{{ rule.id }}" class="fa fa-fw fa-flask"></span></a>

                                                    {# actually execute rule #}
                                                <a href="{{ route('rules.select-transactions',rule.id) }}" class="btn btn-default" title=" {{ trans('firefly.apply_rule_selection', {title: rule.title}) }}"><span class="fa fa-fw fa-power-off "></span></a>
                                                {% endif %}

                                                {# duplicate rule #}
                                                <a href="#" class="btn btn-default duplicate-rule" data-id="{{ rule.id }}" title=" {{ trans('firefly.duplicate_rule', {title: rule.title}) }}"><span class="fa fa-fw fa-copy"></span></a>
                                            </div>
                                        </td>
                                        <td class="markdown">
                                            {% if rule.active %}
                                                {{ rule.title }}
                                            {% else %}
                                                <s>{{ rule.title }}</s> ({{ 'inactive'|_|lower }})
                                            {% endif %}
                                            {% if rule.stop_processing %}
                                                <span class="fa fa-stop-circle-o"></span>
                                            {% endif %}

                                            {% if rule.description != "" %}
                                                <small class="hidden-xs"
                                                        {% if not rule.active %}
                                                            class="text-muted"
                                                        {% endif %}
                                                ><br/>{{ rule.description|default('')|markdown }}</small>
                                            {% endif %}
                                            <small><br />{% if rule.strict %}<span class="text-danger">{{ 'rule_is_strict'|_ }}</span>{% else %}<span class="text-success">{{ 'rule_is_not_strict'|_ }}</span>{% endif %}</small>
                                        </td>
                                        <td class="hidden-xs">
                                            {% if rule.ruleTriggers.count() > 0 %}
                                                <ul class="small" data-id="{{ rule.id }}">
                                                    {% for trigger in rule.ruleTriggers %}
                                                        {% if trigger.trigger_type != "user_action" %}
                                                            <li
                                                                    {% if not rule.active %}
                                                                        class="text-muted"
                                                                    {% endif %}
                                                                    data-id="{{ trigger.id }}">
                                                                {{ trans(('firefly.rule_trigger_' ~ getRootSearchOperator(trigger.trigger_type)), {trigger_value: trigger.trigger_value}) }}

                                                                {% if trigger.stop_processing %}
                                                                    <span class="fa fa-stop-circle-o"></span>
                                                                {% endif %}
                                                            </li>
                                                        {% endif %}
                                                    {% endfor %}
                                                </ul>
                                            {% endif %}
                                        </td>
                                        <td class="hidden-xs">
                                            {% if rule.ruleActions.count() > 0 %}
                                                <ul class="small" data-id="{{ rule.id }}">
                                                    {% for action in rule.ruleActions %}
                                                        <li
                                                                {% if not rule.active %}
                                                                    class="text-muted"
                                                                {% endif %}
                                                                data-id="{{ action.id }}">{{ trans(('firefly.rule_action_' ~ action.action_type), {action_value: action.action_value}) }}
                                                            {% if action.stop_processing %}
                                                                <span class="fa fa-stop-circle-o"></span>
                                                            {% endif %}
                                                        </li>
                                                    {% endfor %}
                                                </ul>
                                            {% endif %}
                                        </td>
                                    </tr>
                                {% endfor %}
                                </tbody>
                            </table>
                        {% else %}
                            <p>
                                <em>{{ 'no_rules_in_group'|_ }}</em>
                            </p>
                        {% endif %}
                        <p>
                            <br/>
                            <a href="{{ route('rules.create', ruleGroup.id) }}"
                               class="btn btn-success new_rule">{{ 'new_rule'|_ }}</a>
                        </p>
                    </div>
                </div>
            </div>
        </div>
    {% endfor %}

    {% include 'rules.partials.test-trigger-modal' %}

    <div class="row">
        <div class="col-lg-12 col-md-12 col-sm-12">
            <a href="{{ route('rule-groups.create') }}" class="btn btn-success">{{ 'new_rule_group'|_ }}</a>
        </div>
    </div>

{% endblock %}
{% block scripts %}
    <script type="text/javascript" nonce="{{ JS_NONCE }}">
        var moveRuleGroupUrl = '{{ route('rule-groups.move') }}';
        var duplicateRuleUrl = '{{ route('rules.duplicate') }}';
    </script>
    <script type="text/javascript" src="v1/js/lib/jquery-ui.min.js?v={{ FF_VERSION }}" nonce="{{ JS_NONCE }}"></script>
    <script type="text/javascript" src="v1/js/ff/rules/index.js?v={{ FF_VERSION }}" nonce="{{ JS_NONCE }}"></script>
{% endblock %}
